package com.solace.selenium.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.solace.selenium.entity.JsonResult;
import com.solace.selenium.entity.VideoConf;
import com.solace.selenium.entity.VideoGroup;
import com.solace.selenium.entity.VideoOps;
import com.solace.selenium.service.TaskService;
import com.solace.selenium.service.VideoConfService;
import com.solace.selenium.service.VideoGroupService;
import com.solace.selenium.service.VideoOpsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("video")
public class VideoController {
    @Autowired
    VideoGroupService videoGroupService;
    @Autowired
    VideoConfService videoConfService;
    @Autowired
    TaskService taskService;
    @Autowired
    VideoOpsService videoOpsService;

    //跳转
    @RequestMapping(value = "save")
    public JsonResult save(VideoGroup videoGroup) {
        videoGroupService.save(videoGroup);
        return new JsonResult("OK");
    }

    /**
     * 作者 CG
     * 时间 2021/8/12 23:15
     * 描述  创建分组
     */
    @RequestMapping(value = "addVideoGroup")
    public JsonResult addVideoGroup(String name, String note, String ops) {
        Assert.hasText(name, "分组名字不能为空");
        Assert.hasText(ops, "操作内容不能为空");
        List<VideoOps> videoOps = JSONArray.parseArray(ops, VideoOps.class);
        videoGroupService.addVideoGroup(name, note, videoOps);
        return new JsonResult("OK");
    }

    /**
     * 作者 CG
     * 时间 2021/8/12 23:25
     * 描述  分页查询
     */
    @RequestMapping(value = "findList")
    public JsonResult findList(@RequestParam(value = "current", defaultValue = "1") Long current, @RequestParam(value = "pageSize", defaultValue = "20") Long pageSize) {
        IPage<VideoGroup> list = videoGroupService.findList(current, pageSize);
        return new JsonResult(list);
    }

    /**
     * 作者 CG
     * 时间 2021/8/12 23:28
     * 描述  查询详细
     */
    @RequestMapping(value = "findByIdAndGroupName")
    public JsonResult findByIdAndGroupName(String groupId) {
        Assert.hasText(groupId, "分组ID不能为空");
        JSONObject byIdAndGroupName = videoGroupService.findByIdAndGroupName(groupId);
        return new JsonResult(byIdAndGroupName);
    }

    /**
     * 作者 CG
     * 时间 2021/8/13 23:28
     * 描述  添加配置并启动
     */
    @RequestMapping(value = "addVideoConf")
    public JsonResult addVideoConf(String conf) {
        Assert.hasText(conf, "分组ID不能为空");
        List<VideoConf> videoConfs = JSONArray.parseArray(conf, VideoConf.class);
        videoConfService.addVideoConf(videoConfs);
        return new JsonResult("保存成功");
    }

    /**
     * 作者 CG
     * 时间 2021/8/13 23:31
     * 描述  启动项目
     */
    @RequestMapping(value = "startVideoConf")
    public JsonResult startVideoConf(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime dateTime) {
        Assert.notNull(dateTime, "启动时间不能为空");
        videoConfService.startVideoConf(dateTime);
        return new JsonResult("启动成功");
    }

    /**
     * 作者 CG
     * 时间 2021/8/13 23:32
     * 描述  查询配置
     */
    @RequestMapping(value = "findAll")
    public JsonResult findAll() {
        List<VideoConf> all = videoConfService.findAll();
        return new JsonResult(all);
    }

    /**
     * 作者 CG
     * 时间 2021/8/13 23:38
     * 描述  删除分组信息
     */
    @RequestMapping(value = "delByGroup")
    public JsonResult delByGroup(String groupId) {
        Assert.hasText(groupId, "删除的分组ID不能为空");
        videoGroupService.delGroup(groupId);
        return new JsonResult("删除成功");
    }

    /**
     * 作者 CG
     * 时间 2021/8/13 23:53
     * 描述  更新分组信息
     */
    @RequestMapping(value = "updateVideoGroup")
    public JsonResult updateVideoGroup(String groupId, String name,String note, String ops) {
        Assert.hasText(groupId, "分组ID不能为空");
        Assert.hasText(ops, "操作内容不能为空");
        List<VideoOps> videoOps = JSONArray.parseArray(ops, VideoOps.class);
        videoGroupService.updateVideoGroup(groupId, name,note, videoOps);
        return new JsonResult("OK");
    }

    /**
     * 作者 CG
     * 时间 2021/8/14 0:26
     * 描述  停止操作
     */
    @RequestMapping(value = "stop")
    public JsonResult stop() {
        taskService.stop();
        return new JsonResult("停止成功");
    }

    /**
     * 作者 CG
     * 时间 2021/8/14 2:52
     * 描述  操作类型
     */
    @RequestMapping(value = "getTaskType")
    public JsonResult getTaskType() {
        JSONArray taskType = videoOpsService.getTaskType();
        return new JsonResult(taskType);
    }

    /**
     * 作者 CG
     * 时间 2021/8/14 2:54
     * 描述  获取配置类型
     */
    @RequestMapping(value = "getConfType")
    public JsonResult getConfType() {
        JSONArray confType = videoOpsService.getConfType();
        return new JsonResult(confType);
    }

    /**
     * 作者 CG
     * 时间 2021/8/15 17:06
     * 描述  复制分组信息
     */
    @RequestMapping(value = "copyByIdVideoGroup")
    public JsonResult copyByIdVideoGroup(String groupId) {
        videoGroupService.copyByIdVideoGroup(groupId);
        return new JsonResult("复制成功");
    }
}
